from PyQt5.QtWidgets import *
from PyQt5 import uic

import smtplib
from email import encoders
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart


class MyGUI(QMainWindow):

	def __init__(self):
		super(MyGUI, self).__init__()
		uic.loadUi('Main.ui', self)
		self.show()

		self.pushButton.clicked.connect(self.login)
		self.pushButton_2.clicked.connect(self.attach_sth)
		self.pushButton_3.clicked.connect(self.send_mail)


	def login(self):
		try:
			self.server = smtplib.SMTP(self.lineEdit_3.text(), self.lineEdit_4.text())
			self.server.ehlo()
			self.server.starttls()
			self.server.ehlo()
			self.server.login(self.lineEdit.text(), self.lineEdit_2.text())

			self.lineEdit.setEnabled(False)
			self.lineEdit_2.setEnabled(False)
			self.lineEdit_3.setEnabled(False)
			self.lineEdit_4.setEnabled(False)
			self.pushButton.setEnabled(False)

			self.lineEdit_5.setEnabled(True)
			self.lineEdit_6.setEnabled(True)
			self.textEdit.setEnabled(True)
			self.pushButton_2.setEnabled(True)
			self.pushButton_3.setEnabled(True)

			self.msg = MIMEMultipart()
		except smtplib.SMTPAuthenticationError:
			massage_box = QMessageBox()
			massage_box.setText('Недопустимые данные!')
			massage_box.exec()
		except:
			massage_box = QMessageBox()
			massage_box.setText('Не удалось выполнить вход!')
			massage_box.exec()

	def attach_sth(self):
		options = QFileDialog.Options()
		filenames, _ = QFileDialog.getOpenFileNames(self, 'Open File', '', 'All Files (*.*)', options=options)
		if filenames != []:
			for filename in filenames:
				attachment = open(filename, 'rb')

				filename = filename[filename.rfind('/') + 1:]

				p = MIMEBase('application', 'octet-stream')
				p.set_payload(attachment.read())
				encoders.encode_base64(p)
				p.add_header('Content-Disposition', f'attachment; filename={filename}')
				self.msg.attach(p)
				if not self.label_8.text().endswith(': '):
					self.label_8.setText(self.label_8.text() + ',')
				self.label_8.setText(self.label_8.text() + ' ' + filename)

	def send_mail(self):
		dialog = QMessageBox()
		dialog.setText('Вы хотите отправить сообщение?')
		dialog.addButton(QPushButton('Да'), QMessageBox.YesRole)
		dialog.addButton(QPushButton('Нет'), QMessageBox.NoRole)

		if dialog.exec_() == 0:
			try:
				self.msg['From'] = 'Xakep'
				self.msg['To'] = self.lineEdit_6.text()
				self.msg['Subject'] = self.lineEdit_5.text()
				self.msg.attach(MIMEText(self.textEdit.toPlainText(), 'plain'))
				text = self.msg.as_string()
				self.server.sendmail(self.lineEdit.text(), self.lineEdit_6.text(), text)
				massage_box = QMessageBox()
				massage_box.setText('Сообщение отправлено!')
				massage_box.exec()

			except:
				massage_box = QMessageBox()
				massage_box.setText('Не удалось отправить сообщение!')
				massage_box.exec()

app = QApplication([])
windows = MyGUI()
app.exec_()